/* 
使用格式:
    1.引入公用样式表
    2.引入当前样式表
    3.引入公用js模块
    4.具体功能

*/

// 1.引入公用样式表
require("../../assets/css/index.less");
require("../../assets/fonts/iconfont.css");
let local = require("..//../utils/local.js");
const axios = require("axios");

// 2.引入当前样式表
require("./revise.less");

// import axios from "axios";
// import "weui";
require("../../assets/lib/weui/weui.css");

let weui = require("../../assets/lib/weui/weui.min.js");

// 3.引入公用js模块
let dom = require("../../utils/dom.js");

let https = require("../../utils/https.js");

dom.ready(function () {
	let user = local.get("user");
	// https.get("/api/user/info", function (res) {
	// console.log(res.data);
	// dom.getEle(".avatar").src = https.BASE_URL + user.imgUrl;
	// dom.getEle("#useval").value = user.nickName;
	// dom.getEle(".sex").innerHTML = user.gender == 0 ? "男" : "女";
	// dom.getEle("#tareaBtn").value = user.sign;
	// dom.getEle(".shouBair").innerHTML = user.birthday;
	// });

	dom.jump(".my-data", "my");

	//获取
	let imgUrl;
	dom.getEle("#up-photo").addEventListener("change", function () {
		https.upPhoto(
			"/api/shared/uploadPortrait",
			"file",
			this.files[0],
			function (res) {
				if (res.errno === 0) {
					dom.getEle(".avatar").src = https.BASE_URL + res.data.url;
					imgUrl = res.data.url || "../../assets/imgs/cat.jpg";
				}
			}
		);

		// //获取文件
		// let f = this.files[0];

		// //
		// let fd = new FormData()

		// fd.append("file", f)

		// https.post("/api/circles/uploadImg", fd, function (res) {
		//     console.log(res);
		//     if (res.errno === 0) {
		//         dom.getEle(".avatar").src = https.BASE_URL + res.data.url
		//     }
		// })
	});

	// 性别
	let sex;
	dom.getEle(".sex").addEventListener("click", function () {
		weui.picker(
			[
				{
					label: "男",
					value: 0,
				},
				{
					label: "女",
					value: 1,
				},
			],
			{
				onConfirm: (result) => {
					console.log((this.innerHTML = result[0].label));
					sex = result[0] || 0;
				},
				title: "选择你的性别",
			}
		);
	});

	//生日
	dom.getEle(".shouBair").addEventListener("click", function () {
		weui.datePicker({
			start: 1990,
			end: new Date().getFullYear(),

			onConfirm: (result) => {
				this.innerHTML = result
					.map((v) => {
						return v.value < 10 ? "0" + v.value : v.value;
					})
					.join("-");
			},
			title: "选择你的生日",
		});
	});

	//所在城市
	let provinceIda = {
		label: "北京",
		value: 110000,
	};
	let showCityA = {
		label: "东城区",
		value: 110100,
	};

	dom.getEle("#showCity").addEventListener("click", function () {
		https.get("/api/shared/province", (res) => {
			console.log(res);
			showCityA = res.data.map((v) => {
				return {
					label: v.name,
					value: v.id,
				};
			});
			weui.picker(showCityA, {
				onConfirm: (result) => {
					console.log(result[0]);
					this.innerHTML = result[0].label;
					provinceIda = result[0];
				},
				title: "选择你所在的省",
			});
		});
	});

	//所在市

	let ctryb;
	dom.getEle("#city").addEventListener("click", function () {
		// console.log(provinceId);
		https.get("/api/shared/city?provinceId=" + provinceIda, function (res) {
			// console.log(res.data);
			let b = res.data.map(function (v) {
				return {
					label: v.name,
					value: v.id,
				};
			});
			ctryb = b;
			weui.picker(ctryb, {
				onConfirm: (result) => {
					dom.getEle("#city").innerHTML = result[0].label;
					showCityA = result[0];
				},
				title: "选择你所在的省",
			});
		});
	});
	dom.getEle("#tareaBtn").addEventListener("input", function () {
		// console.log(11);
		let len = this.value.length;
		if (len > 10) {
			this.value = this.value.slice(0, 10);
		} else {
			dom.getEle("#textNum").innerHTML = len;
		}
	});
	dom.getEle("#reviseBtn").addEventListener("click", function () {
		// let shuju = {
		// 	imgUrl: imgUrl,
		// 	nickName: dom.getEle("#useval").value || "空",
		// 	gender: sex,
		// 	birthday: dom.getEle(".shouBair").innerHTML,
		// 	provinceId: provinceIda,
		// 	cityId: ctryb,
		// 	sign: dom.getEle("#tareaBtn").value || "该用户什么都没有输入",
		// };
		https.post(
			"/api/user/changeInfo",
			{
				imgUrl: imgUrl,
				nickName: dom.getEle("#useval").value || "空",
				gender: sex,
				birthday: dom.getEle(".shouBair").innerHTML,
				provinceId: provinceIda.value,
				cityId: showCityA.value,
				sign: dom.getEle("#tareaBtn").value || "该用户什么都没有输入",
			},
			function (res) {
				console.log(res);
				if (res.errno == 0) {
					console.log(res);
					location.href = "my.html";
					local.set("tk", res.data.token);
					local.set("userin", res.data);
				} else {
					// weui.alert("没有修改信息");
				}
			}
		);
	});
});
